package router

import (
	"enjoy_xxz/internal/api"
	"enjoy_xxz/internal/api/goods"
	"enjoy_xxz/internal/api/order"
	user2 "enjoy_xxz/internal/api/user"
	"enjoy_xxz/pkg"
	"enjoy_xxz/pkg/wx"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func Router(r *gin.Engine) {
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"*"},
		AllowHeaders:     []string{"*"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "https://github.com"
		},
		MaxAge: 12 * time.Hour,
	}))
	// 添加微信签名验证路由
	r.GET("/", wx.ProcSignature)
	// 注册微信登录回调路由
	http.HandleFunc("/auth/callback", pkg.WechatLoginCallback)
	//不许要认证的路由
	u := r.Group("/user")
	{
		u.POST("/login", user2.Login)
		u.POST("/register", user2.Register)
		u.POST("/send/code", user2.SendCode)
	}
	//需要认证中间件的路由
	au := r.Group("/user", pkg.JWTAuth("2206A"))
	{
		au.POST("/update", user2.UpdatePasswordByUserID)
		au.POST("/create/addr", user2.CreateAddr)
		au.POST("/addr/list", user2.GetAddrListByUserId)
		au.POST("/addr/detail", user2.GetAddrDetail)
		au.POST("/addr/get/default", user2.GetAddrDefault) //获取默认地址
	}
	//商品
	g := r.Group("/goods")
	{
		g.POST("/detail", goods.GoodsDetail)                          //商品详情
		g.POST("/sku/info", goods.GetSkuInfo)                         //商品sku信息
		g.POST("/search/list", goods.SearchGoodsList)                 //商品搜索列表
		g.POST("/type/list", goods.CategoryByIDGoodsInfo)             //分类id查询商品列表
		g.POST("/category/list", goods.CategoryList)                  //商品分类列表
		g.POST("/category/location/list", goods.CategoryLocationList) //商品分类位置列表
	}
	//订单
	o := r.Group("/order", pkg.JWTAuth("2206A"))
	{
		o.POST("/create", order.CreateOrder)
		o.POST("/list", order.OrderList)
		o.POST("/goods/sku/list", order.GetGoodsSkuInfoBySkuID)
		o.POST("/goods/id/list", order.GetOrderGoodsById)
	}
	//购物车
	c := r.Group("/cart", pkg.JWTAuth("2206A"))
	{
		c.POST("/add", api.CreateCart)
		c.POST("/list", api.GetCartList)
		c.POST("/update", api.CartUpdateByQuantity)
	}
	//秒杀
	s := r.Group("/spikes")
	{
		s.POST("/activity", api.GetSpikesActivityList)
	}
}
